import axios from 'axios'
import { getToken } from '@/utils/auth'


axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const service = axios.create({
  baseURL: '',
  timeout: 10000
})

service.interceptors.request.use(config => {
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  if (getToken() && !isToken) {
    config.headers['Admin-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  return config
}, error => {
    console.log(error)
  Promise.reject(error)
});

const vm = this;
service.interceptors.response.use(function (response) {
  console.log(response);
  return response;
}, function (error) {
  if (error.response && error.response.status === 401) {
    vm.$router.replace('/');
  }
  return Promise.reject(error);
});
export default service
